// pages/shopsetting/shopsetting.js
var app = getApp();
var utils = require('../../utils/shop/formatShop.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    Shop_Logo:'',
    Shop_Name:'',
    upload_img_path: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideLoading();
    //检查用户是否登录
    if (!app.globalData.userInfo) {
      app.login('', this.load);
    } else {
      this.load();
    }
  },

  //页面加载
  load: function () {
    var dataArgs = {
      act: 'get_user_dis_config',
      User_ID: app.globalData.userInfo.User_ID
    };
    app.http_req(dataArgs, app.globalData.init.api_url, 'POST', this.handledisaccount_callback);
  },

  //根据User_ID编辑和获取分销信息接口回调
  handledisaccount_callback: function (res) {
    if (res.errorCode === 0) {
      this.setData({
        Shop_Logo: res.data.Shop_Logo ? (res.data.Shop_Logo.indexOf('http') > -1 ? res.data.Shop_Logo : app.globalData.init.front_url + res.data.Shop_Logo) : '../../images/moren.png',
        Shop_Name: res.data.Shop_Name
      })   
    }
  },

  //上传图片
  uploadFile: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        var image = res.tempFilePaths[0];
        
        var formData = {
          act: 'upload_image',
          // uri: '/api/little_program/shopconfig.php',
          appid: app.globalData.init.appid,
       //   Users_Account: app.globalData.init.Users_Account,
          User_ID: app.globalData.userInfo.User_ID
        }
        //添加签名
        formData = app.createToken(formData);
        //上传
        wx.uploadFile({
          url: app.globalData.init.api_url,
          filePath: image,
          name: 'image',
          formData: formData,
          success: function (res2) {
            var callback_res = JSON.parse(res2.data);
            if (callback_res.errorCode == 0) {
              //保存要上传的图片
              that.setData({
                Shop_Logo: image,
                upload_img_path: callback_res.path
              })
            } else {
              wx.showModal({
                title: '提示',
                content: typeof callback_res.msg != 'undefined' ? callback_res.msg : '上传失败',
                showCancel: false
              })
            }
          }
        });
      },
    })
  },

  //提交设置
  formSubmit: function (form) {
    var form_info = form.detail.value;
    //判断
    if (!form_info.Shop_Name) {
      wx.showModal({
        title: '提示',
        content: '请填写店铺名称',
        showCancel: false
      })
      return false;
    }

    var dataArgs = {
      act: 'updata_user_dis_config',
      User_ID: app.globalData.userInfo.User_ID,
      Shop_Name: form_info.Shop_Name
    };
    if (this.data.upload_img_path) {
      dataArgs.Shop_Logo = this.data.upload_img_path;
    }  
    /**
     * 更新店铺信息
     */ 
    app.http_req(dataArgs, app.globalData.init.api_url, 'POST', this.shopsubmit_callback);
  },

  //写入分销商提现记录回调
  shopsubmit_callback: function (res) {
    if (res.errorCode === 0) {
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 1000,
        success: function () {
          setTimeout(function () {
            //返回上一页
            wx.navigateBack({
              delta: 1
            });
          }, 1000);
        }
      });
    } else {
      wx.showModal({
        title: '提示',
        content: res.msg,
        showCancel: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})